package com.ktgj.customer.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.dto.ProductListDto;
import com.ktgj.customer.mapper.TravelContractMapper;
import com.ktgj.customer.mapper.TravelGroupMapper;
import com.ktgj.customer.mapper.TravelSupplierMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.TravelProductMapper;
import com.ktgj.customer.service.ITravelProductService;

/**
 * 旅游产品Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-26
 */
@Service
public class TravelProductServiceImpl implements ITravelProductService 
{
    @Autowired
    private TravelProductMapper travelProductMapper;
    @Autowired
    private TravelGroupMapper travelGroupMapper;
    @Autowired
    private TravelSupplierMapper travelSupplierMapper;
    @Autowired
    private TravelContractMapper travelContractMapper;

    /**
     * 查询旅游产品
     * 
     * @param productId 旅游产品ID
     * @return 旅游产品
     */
    @Override
    public TravelProductDto selectTravelProductById(Long productId)
    {
        return travelProductMapper.selectTravelProductById(productId);
    }

    /**
     * 查询旅游产品列表
     * 
     * @param travelProductDto 旅游产品
     * @return 旅游产品
     */
    @Override
    public List<TravelProductDto> selectTravelProductList(TravelProductDto travelProductDto)
    {
        return travelProductMapper.selectTravelProductList(travelProductDto);
    }

    /**
     * 新增旅游产品
     * 
     * @param travelProductDto 旅游产品
     * @return 结果
     */
    @Override
    public int insertTravelProduct(TravelProductDto travelProductDto)
    {
        travelProductDto.setCreateTime(DateUtils.getNowDate());
        return travelProductMapper.insertTravelProduct(travelProductDto);
    }

    /**
     * 修改旅游产品
     * 
     * @param travelProductDto 旅游产品
     * @return 结果
     */
    @Override
    public int updateTravelProduct(TravelProductDto travelProductDto)
    {
        return travelProductMapper.updateTravelProduct(travelProductDto);
    }

    /**
     * 批量删除旅游产品
     * 
     * @param productIds 需要删除的旅游产品ID
     * @return 结果
     */
    @Override
    public int deleteTravelProductByIds(Long[] productIds)
    {
        return travelProductMapper.deleteTravelProductByIds(productIds);
    }

    /**
     * 删除旅游产品信息
     * 
     * @param productId 旅游产品ID
     * @return 结果
     */
    @Override
    public int deleteTravelProductById(Long productId)
    {
        return travelProductMapper.deleteTravelProductById(productId);
    }

    /**
     * 团产品列表
     * */
    @Override
    public List<TravelGroup> productList(ProductListDto productListDto) {
        List<TravelGroup> travelGroups = travelGroupMapper.productList(productListDto);
        List<Long> supplierIdList = new ArrayList<>();
        if (productListDto.getSupplierName() != null && !productListDto.getSupplierName().equals("")){
            List<TravelSupplier> travelSuppliers = travelSupplierMapper.productList(productListDto.getSupplierName());
            if (travelSuppliers != null && travelSuppliers.size() > 0){
                for (TravelSupplier travelSupplier : travelSuppliers) {
                    supplierIdList.add(travelSupplier.getSupplierId());
                }
            }
        }
        TravelContractDto queryContract = new TravelContractDto();
        List<TravelContractDto> lstContract = travelContractMapper.selectTravelContractList(queryContract);
        TravelSupplierDto querySupplier = new TravelSupplierDto();
        List<TravelSupplierDto> lstSupplier = travelSupplierMapper.selectTravelSupplierList(querySupplier);
        productListDto.setSupplierIdList(supplierIdList);
        for (TravelGroup travelGroup :travelGroups)
        {
            TravelSupplierDto travelSupplierDto = lstSupplier.stream().filter(item->item.getSupplierId().equals(travelGroup.getSupplierId())).findFirst().orElse(null);
            travelGroup.setSupplierName(travelSupplierDto.getSupplierName());
            TravelContractDto travelContractDto = lstContract.stream().filter(item->item.getContractId().equals(travelGroup.getContractId())).findFirst().orElse(null);
            travelGroup.setContractNumber(travelContractDto.getContractNum());
        }
        return travelGroups;
    }
}
